{% extends 'base.html' %}
{% load static %}
{% load i18n %}
{% load common_tags %}

{% block custom_head_css_js %}
    <link href="{% static 'css/plugins/ladda/ladda-themeless.min.css' %}" rel="stylesheet">
    <link href="{% static "css/plugins/footable/footable.core.css" %}" rel="stylesheet">
{% endblock %}
{% block content %}
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="panel-options">
                        <ul class="nav nav-tabs">
                            <li class="active">
                                <a class="text-center" href="{% url 'terminal:session-detail' pk=object.id %}"> {% trans 'Session detail' %} </a>
                            </li>
                            <li>
                                <a class="text-center" href="{% url 'terminal:session-commands' pk=object.id %}"> {% trans 'Commands' %} </a>
                            </li>
                        </ul>
                    </div>
                    <div class="tab-content">
                        <div class="col-sm-8" style="padding-left: 0;">
                            <div class="ibox float-e-margins">
                                <div class="ibox-title">
                                    <span style="float: left"> <b>{{ object.id }}</b></span>
                                    <div class="ibox-tools">
                                        <a class="collapse-link">
                                            <i class="fa fa-chevron-up"></i>
                                        </a>
                                        <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                                            <i class="fa fa-wrench"></i>
                                        </a>
                                        <ul class="dropdown-menu dropdown-user">
                                        </ul>
                                        <a class="close-link">
                                            <i class="fa fa-times"></i>
                                        </a>
                                    </div>
                                </div>
                                <div class="ibox-content">
                                    <table class="table">
                                        <tbody>
                                        <tr class="no-borders-tr">
                                            <td width="20%">{% trans 'User' %}:</td>
                                            <td><b>{{ object.user }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Asset' %}:</td>
                                            <td><b>{{ object.asset }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'System user' %}:</td>
                                            <td><b>{{ object.system_user }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Protocol' %}:</td>
                                            <td><b>{{ object.protocol }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Login from' %}:</td>
                                            <td><b>{{ object.login_from_display }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Remote addr' %}:</td>
                                            <td><b>{{ object.remote_addr }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Date start' %}:</td>
                                            <td><b>{{ object.date_start }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Date end' %}:</td>
                                            <td><b>{{ object.date_end }}</b></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                        <div class="col-sm-4" style="padding-left: 0;padding-right: 0">
                            <div class="panel panel-primary">
                                <div class="panel-heading">
                                    <i class="fa fa-info-circle"></i> {% trans 'Quick modify' %}
                                </div>
                                <div class="panel-body">
                                    <table class="table">
                                        <tbody>
                                        {% if object.is_finished %}
                                        <tr class="no-borders-tr">
                                            <td>{% trans 'Replay session' %}:</td>
                                            <td>
                                                <span class="pull-right">
                                                    <button type="button" onclick="window.open('/luna/replay/{{ object.id }}','luna', 'height=600, width=800, top=400, left=400, toolbar=no, menubar=no, scrollbars=no, location=no, status=no')" class="btn btn-primary btn-xs" id="btn_reset_password" style="width: 54px">{% trans 'Go' %}</button>
                                                </span>
                                            </td>
                                        </tr>
                                            <tr>
                                                <td>{% trans 'Download replay' %}:</td>
                                                <td>
                                                <span class="pull-right">
                                                    <button class="ladda-button btn btn-primary btn-xs btn-download" style="width: 54px" data-style="zoom-in" >{% trans 'Download' %}</button>
                                                </span>
                                                </td>
                                            </tr>
                                        {% else %}
                                        <!--<tr>-->
                                            <!--<td class="no-borders" >{% trans 'Monitor session' %}:</td>-->
                                            <!--<td class="no-borders" >-->
                                                <!--<span class="pull-right">-->
                                                    <!--<button type="button" class="btn btn-primary btn-xs " style="width: 54px">{% trans 'Go' %}</button>-->
                                                <!--</span>-->
                                            <!--</td>-->
                                        <!--</tr>-->
                                        <tr class="no-borders-tr">
                                            <td>{% trans 'Terminate session' %}:</td>
                                            <td>
                                                <span class="pull-right">
                                                    <button type="button" class="btn btn-primary btn-xs btn-term" value="{{ object.id }}" terminal="{{ object.terminal.id }}"  style="width: 54px;">{% trans 'Confirm' %}</button>
                                                </span>
                                            </td>
                                        </tr>
                                        {% endif %}
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

{% endblock %}
{% block custom_foot_js %}
<script src="{% static "js/plugins/ladda/spin.min.js" %}"></script>
<script src="{% static "js/plugins/ladda/ladda.min.js" %}"></script>
<script src="{% static "js/plugins/ladda/ladda.jquery.min.js" %}"></script>
<script>
    var downloadPageUrl = "{% url "terminal:session-replay-download" pk=object.id %}";
    var sessionReplayDetailUrl = '{% url "api-terminal:session-replay" pk=object.id %}';
    var interval = null;
    var l;

    function terminateSession(data) {
        function success() {
            window.setTimeout(function () {
                window.location.reload()
            }, 300)
        }
        var the_url = "{% url 'api-terminal:tasks-list' %}";
        requestApi({
            url: the_url,
            method: 'POST',
            body: JSON.stringify(data),
            success: success,
            success_message: "{% trans 'Terminate success'%}"
        });
    }

    function getReplayDetail() {
        function success(data) {
            if (data.src) {
                clearInterval(interval);
                l.ladda('stop');
                window.open(downloadPageUrl);
                return;
            }
            if (data.status === "error") {
                clearInterval(interval);
                l.ladda('stop');
                toastr.error(data.error)
            }
        }

        requestApi({
            url: sessionReplayDetailUrl,
            method: 'GET',
            success: success,
            flash_message: false
        })
    }

    $(document).ready(function () {
        l = $('.btn-download').ladda();
        l.click(function () {
            l.ladda('start');
            interval = setInterval(getReplayDetail, 2000)
        });
        var downloadDirect = getUrlParam('download');
        if (downloadDirect) {
            $('.btn-download').trigger('click');
        }
    }).on('click', '.btn-term', function () {
        var $this = $(this);
        var session_id = $this.attr('value');
        var terminal_id = $this.attr('terminal');
        alert(session_id);
        var data = {
            name: "kill_session",
            args: session_id,
            terminal: terminal_id
        };
        terminateSession(data)
    })
</script>
{% endblock %}
